package com.topisv.tms.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.SubWarehouseRel;
import com.topisv.tms.exception.ErrorCode;
import com.topisv.tms.exception.PaiUException;
import com.topisv.tms.repository.AreaGroupDao;
import com.topisv.tms.repository.SubWarehouseRelDao;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;


@Component
@Transactional(readOnly = true)
public class SubWarehouseRelService {

	@Autowired
	private SubWarehouseRelDao subWarehouseRelDao;
	@Autowired
	private AreaGroupDao areaGroupDao;
	
	private static final Logger logger = LoggerFactory.getLogger(SubWarehouseRelService.class);

	
	
	public List<SubWarehouseRel> findAll() {
		return (List<SubWarehouseRel>) subWarehouseRelDao.findAll();
	}

	
	public List<SubWarehouseRel> findByMainWarehouseCode(String mainWarehouseCode) {
		return subWarehouseRelDao.findByMainWarehouseCode(mainWarehouseCode);
	}
	
	@Transactional(readOnly = false)
	public void assignSubWarehouse(String mainWarehouseCode,String[] subWarehouseInfos) throws Exception {
		AreaGroup areaGroup = areaGroupDao.findByGroupCode(mainWarehouseCode);
		if(areaGroup==null){
			logger.info("主仓编码不存在"+mainWarehouseCode);
			throw new PaiUException(ErrorCode.DATA_ERROR);
		}
		logger.info("删除所有分仓");
		subWarehouseRelDao.deleteByMainWarehouseCode(mainWarehouseCode);
		
		
		List<SubWarehouseRel> allAssign = (List<SubWarehouseRel>) subWarehouseRelDao.findAll();
		for(String subWarehouseArr:subWarehouseInfos){
			String[] subWarehouseInfo = subWarehouseArr.split(":");
			String subWarehouseCode = subWarehouseInfo[0];
			if(StringUtils.isEmpty(subWarehouseCode)){
				continue;
			}
			for(SubWarehouseRel rel:allAssign){
//				if(mainWarehouseCode.equals(rel.getMainWarehouseCode())){
//					continue;
//				}
				if(subWarehouseCode.equals(rel.getSubWarehouseCode())){
					logger.info("子仓已经被分配为其他子仓");
					throw new PaiUException(ErrorCode.DATA_ERROR);
				}
				if(subWarehouseCode.equals(rel.getMainWarehouseCode())){
					logger.info("子仓已经是总仓不能被分为子仓");
					throw new PaiUException(ErrorCode.DATA_ERROR);
				}
			}
		}
		
		ShiroUser curUser = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		
		
		
		List<SubWarehouseRel> insertList = new ArrayList<SubWarehouseRel>();
		for(String subWarehouseArr:subWarehouseInfos){
			String[] subWarehouseInfo = subWarehouseArr.split(":");
			String subWarehouseCode = subWarehouseInfo[0];
			if(StringUtils.isEmpty(subWarehouseCode)){
				continue;
			}
			String subWarehousePriority = subWarehouseInfo[1];
			
			SubWarehouseRel insertRel = new SubWarehouseRel();
			insertRel.setMainWarehouseCode(mainWarehouseCode);
			insertRel.setSubWarehouseCode(subWarehouseCode);
			
			insertRel.setPriority((StringUtils.isEmpty(subWarehousePriority)?999:Integer.parseInt(subWarehousePriority)));
			insertRel.setCreateBy(curUser.jobuuid);
			insertRel.setCreateTime(new Date());
			insertRel.setModifyBy(curUser.jobuuid);
			insertRel.setModifyTime(new Date());
			insertList.add(insertRel);
		}
		logger.info("保存分仓信息");
		subWarehouseRelDao.save(insertList);
	}
	
}
